<template>
	<view>
		<canvas v-show="showCharts" canvas-id="HpeaMrhgegbvTMMquCDyxSYtNFCHBaMF" id="HpeaMrhgegbvTMMquCDyxSYtNFCHBaMF" class="charts"
			@touchend="tap" />
	</view>
</template>

<script>
	import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
	var uChartsInstance = {};
	export default {
		props: {
			showCharts: {
				type: Boolean,
				default () {
					return true;
				}
			},
		},
		data() {
			return {
				cWidth: 750,
				cHeight: 600
			};
		},
		onReady() {
			//这里的 750 对应 css .charts 的 width
			this.cWidth = uni.upx2px(750);
			//这里的 500 对应 css .charts 的 height
			this.cHeight = uni.upx2px(600);
			this.getServerData();
		},
		methods: {
			getServerData() {
				uni.$u.http.get(`/device/mobile/deviceRepair/deviceFaultCensusByMonth`).then(response => {
					if (response.code == 0) {
						let series = [{
							name: '设备故障台数',
							data: []
						}, {
							name: '维修故障数量',
							data: []
						}, {
							name: '维保故障数量',
							data: []
						}, {
							name: '点检故障数量',
							data: []
						}, ]
						response.data.forEach(item => {
							series[0].data.push(item.deviceCount)
							series[1].data.push(item.regularCount)
							series[2].data.push(item.maintainCount)
							series[3].data.push(item.checkCount)
						})
						let res = {
							categories: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
								"12"
							],
							series
						}
						this.drawCharts('HpeaMrhgegbvTMMquCDyxSYtNFCHBaMF', res);
					}
				}).catch(err => {
					uni.$u.toast(err)
				})
			},
			drawCharts(id, data) {
				const ctx = uni.createCanvasContext(id, this);
				uChartsInstance[id] = new uCharts({
					type: "line",
					context: ctx,
					width: this.cWidth,
					height: this.cHeight,
					categories: data.categories,
					series: data.series,
					animation: true,
					timing: "easeOut",
					duration: 1000,
					rotate: false,
					rotateLock: false,
					background: "#FFFFFF",
					color: ["#0F53AC", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 10, 0, 15],
					fontSize: 13,
					fontColor: "#666666",
					dataLabel: true,
					dataPointShape: true,
					dataPointShapeType: "solid",
					touchMoveLimit: 60,
					enableScroll: false,
					enableMarkLine: false,
					legend: {
						show: true,
						position: "bottom",
						float: "center",
						padding: 5,
						margin: 5,
						backgroundColor: "rgba(0,0,0,0)",
						borderColor: "rgba(0,0,0,0)",
						borderWidth: 0,
						fontSize: 13,
						fontColor: "#666666",
						lineHeight: 11,
						hiddenColor: "#CECECE",
						itemGap: 10
					},
					xAxis: {
						disableGrid: true,
						disabled: false,
						axisLine: true,
						axisLineColor: "#CCCCCC",
						calibration: false,
						fontColor: "#666666",
						fontSize: 13,
						lineHeight: 20,
						marginTop: 0,
						rotateLabel: false,
						rotateAngle: 45,
						itemCount: 5,
						boundaryGap: "center",
						splitNumber: 5,
						gridColor: "#CCCCCC",
						gridType: "solid",
						dashLength: 4,
						gridEval: 1,
						scrollShow: false,
						scrollAlign: "left",
						scrollColor: "#A6A6A6",
						scrollBackgroundColor: "#EFEBEF",
						title: "",
						titleFontSize: 13,
						titleOffsetY: 0,
						titleOffsetX: 0,
						titleFontColor: "#666666",
						formatter: ""
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2,
						disabled: false,
						disableGrid: false,
						splitNumber: 5,
						gridColor: "#CCCCCC",
						padding: 10,
						showTitle: false,
						data: []
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow",
							linearType: "none",
							onShadow: false,
							animation: "vertical"
						},
						tooltip: {
							showBox: true,
							showArrow: true,
							showCategory: false,
							borderWidth: 0,
							borderRadius: 0,
							borderColor: "#000000",
							borderOpacity: 0.7,
							bgColor: "#000000",
							bgOpacity: 0.7,
							gridType: "solid",
							dashLength: 4,
							gridColor: "#CCCCCC",
							boxPadding: 3,
							fontSize: 13,
							lineHeight: 20,
							fontColor: "#FFFFFF",
							legendShow: true,
							legendShape: "auto",
							splitLine: true,
							horizentalLine: false,
							xAxisLabel: false,
							yAxisLabel: false,
							labelBgColor: "#FFFFFF",
							labelBgOpacity: 0.7,
							labelFontColor: "#666666"
						},
						markLine: {
							type: "solid",
							dashLength: 4,
							data: []
						}
					}
				});
			},
			tap(e) {
				uChartsInstance[e.target.id].touchLegend(e);
				// uChartsInstance[e.target.id].showToolTip(e);
				uChartsInstance[e.target.id].showToolTip(e, {
					formatter: (item, category, index, opts) => {
						return category + '月' + item.name + ':' + item.data
					}
				});
			}
		}
	};
</script>

<style scoped>
	.charts {
		width: 750rpx;
		height: 600rpx;
		z-index: 0;
	}
</style>